﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using VersionManage.Common;

public static class Extensions//静态类 
{
    #region 验证相关
    #region 判断格式是否为NULL或Empty
    /// <summary>
    /// 判断格式是否为NULL或Empty
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool Ext_IsNullOrEmpty(this string s)//静态方法和this 
    {
        return string.IsNullOrEmpty(s);
    }
    #endregion

    #region 判断格式是否为Email
    /// <summary>
    /// 判断格式是否为Email
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool Ext_IsEmail(this string s)//静态方法和this 
    {
        return Regex.IsMatch(s, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    }
    #endregion
    #region IsURL(string) 检测是否是正确的Url
    /// <summary>
    /// 检测是否是正确的Url
    /// </summary>
    /// <param name="strUrl">要验证的Url</param>
    /// <returns>判断结果</returns>
    public static bool Ext_IsURL(this string strUrl)
    {
        return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
    }
    #endregion

    #region IsNumeric(object) 判断对象是否为Int32类型的数字
    /// <summary>
    /// 判断对象是否为Int32类型的数字
    /// </summary>
    /// <param name="Expression"></param>
    /// <returns></returns>
    public static bool Ext_IsNumeric(this object Expression)
    {
        return TypeParse.IsNumeric(Expression);
    }
    #endregion
    #region IsDouble(object) 判断对象是否为double类型
    /// <summary>
    /// 判断对象是否为double类型
    /// </summary>
    /// <param name="Expression"></param>
    /// <returns></returns>
    public static bool Ext_IsDouble(this object Expression)
    {
        return TypeParse.IsDouble(Expression);
    }
    #endregion
    #region IsInt(string str)验证是否为正整数
    /// <summary>
    /// 验证是否为正整数
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool Ext_IsInt(this string str)
    {
        return Regex.IsMatch(str, @"^[0-9]*$");
    }
    #endregion
    #region IsIP(string ) 是否为ip
    /// <summary>
    /// 是否为ip
    /// </summary>
    /// <param name="ip"></param>
    /// <returns></returns>
    public static bool Ext_IsIP(this string ip)
    {
        return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
    }
    #endregion
    #endregion

    #region 字符串相关操作
    #region Ext_Length(string str) 返回字符串真实长度, 1个汉字长度为2
    /// <summary>
    /// 返回字符串真实长度, 1个汉字长度为2
    /// </summary>
    /// <returns></returns>
    public static int Ext_Length(this string str)
    {
        return Encoding.Default.GetBytes(str).Length;
    }
    #endregion
    #region Ext_GetEmailHostName(string )获取Email主机头
    /// <summary>
    /// 获取Email主机头,包含@
    /// </summary>
    /// <param name="strEmail"></param>
    /// <returns></returns>
    public static string Ext_GetEmailHostName(this string strEmail)
    {
        if (strEmail.IndexOf("@") < 0)
        {
            return "";
        }
        return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();
    }
    #endregion
    #region Ext_RTrim(string str) 删除字符串尾部的回车/换行/空格
    /// <summary>
    /// 删除字符串尾部的回车/换行/空格
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string Ext_RTrim(this string str)
    {
        for (int i = str.Length; i >= 0; i--)
        {
            if (str[i].Equals(" ") || str[i].Equals("\r") || str[i].Equals("\n"))
            {
                str.Remove(i, 1);
            }
        }
        return str;
    }
    #endregion

    #region 字符串截取
    public static string Ext_SubString(this string p_SrcString, int p_Length)
    {
        return p_SrcString.Ext_SubString(p_Length, null);
    }

    /// <summary>
    /// 取指定长度的字符串
    /// </summary>
    /// <param name="p_SrcString">要检查的字符串</param>
    /// <param name="p_StartIndex">起始位置</param>
    /// <param name="p_Length">指定长度</param>
    /// <param name="p_TailString">用于替换的字符串</param>
    /// <returns>截取后的字符串</returns>
    public static string Ext_SubString(this string p_SrcString, int p_Length, string p_TailString)
    {
        if ("" == p_SrcString || null == p_SrcString) { return ""; }
        if (p_SrcString.Ext_Length() <= p_Length)
        {
            return p_SrcString;
        }
        else
        {
            int NewLen = 0;
            for (int i = 0; i < p_Length; i++)
            {
                if (p_SrcString.Substring(NewLen, 1).Ext_Length() > 1)
                    i++;
                NewLen++;
            }
            return p_SrcString.Substring(0, NewLen) + p_TailString;
        }
    }
    #endregion

    public static object Ext_Get(this object[] p_String, int p_length)
    {
        if (null == p_String || p_String.Length <= p_length || p_length < 0) return null;
        return p_String[p_length];
    }

    #region 过滤特殊字符
    #region 过滤
    /// <summary>
    /// 过滤特殊字符
    /// </summary>
    /// <param name="p_String"></param>
    /// <returns></returns>
    public static string Ext_FilterSpecialChar(this string p_String)
    {
        string str = p_String;
        if (string.IsNullOrEmpty(str)) return "";
        str = str.Replace("&", "&amp;");
        str = str.Replace("'", "&#39;");
        str = str.Replace("\"", "&#34;");

        str = Regex.Replace(str, "(w)(here)", "$1h&#101;re", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(s)(elect)", "$1el&#101;ct", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(i)(nsert)", "$1ns&#101;rt", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(c)(reate)", "$1r&#101;ate", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(d)(rop)", "$1ro&#112;", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(a)(lter)", "$1lt&#101;r", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(d)(elete)", "$1el&#101;te", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(u)(pdate)", "$1p&#100;ate", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(\\s)(or)", "$1o&#114;", RegexOptions.IgnoreCase);

        return str;
    }
    #endregion
    #region 反过滤
    /// <summary>
    /// 反过滤特殊字符
    /// </summary>
    /// <param name="p_String"></param>
    /// <returns></returns>
    public static string Ext_UnFilterSpecialChar(this string p_String)
    {
        string str = p_String;
        if (string.IsNullOrEmpty(str)) return "";

        str = Regex.Replace(str, "(\\s)(o&#114;)", "$1or", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(u)(p&#100;ate)", "$1pdate)", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(d)(el&#101;te)", "$1elete", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(a)(lt&#101;r)", "$1lter", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(d)(ro&#112)", "$1rop;", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(c)(r&#101;ate)", "$1reate", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(i)(ns&#101;rt)", "$1nsert)", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(s)(el&#101;ct)", "$1elect", RegexOptions.IgnoreCase);
        str = Regex.Replace(str, "(w)(h&#101;re)", "$1here", RegexOptions.IgnoreCase);

        str = str.Replace("&#34;", "\"");
        str = str.Replace("&#39;", "'");
        str = str.Replace("&amp;", "&");

        return str;
    }
    #endregion
    #endregion

    #region 正则替换
    /// <summary>
    /// 正则替换
    /// </summary>
    /// <param name="p_String"></param>
    /// <param name="SearchString"></param>
    /// <param name="ReplaceString"></param>
    /// <param name="IsCaseInsensetive">是否区分大小写</param>
    /// <returns></returns>
    public static string Ext_Replace(this string p_String, string SearchString, string ReplaceString, bool IsCaseInsensetive)
    {
        if (string.IsNullOrEmpty(SearchString.Trim())) return p_String;
        return Regex.Replace(p_String, SearchString, ReplaceString, IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
    }
    /// <summary>
    /// 正则替换（不区分大小写）
    /// </summary>
    /// <param name="p_String"></param>
    /// <param name="SearchString"></param>
    /// <param name="ReplaceString"></param>
    /// <returns></returns>
    public static string Ext_Replace(this string p_String, string SearchString, string ReplaceString)
    {
        if (string.IsNullOrEmpty(SearchString.Trim())) return p_String;
        if (null == ReplaceString) ReplaceString = "";
        return Regex.Replace(p_String, SearchString, ReplaceString, RegexOptions.IgnoreCase);
    }
    #endregion

    #region Ext_Split(this string str, string s) 字符串分割
    /// <summary>
    /// 字符串分割
    /// </summary>
    /// <param name="str"></param>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string[] Ext_Split(this string str, string s)
    {
        if (str.ToLower().IndexOf(s.ToLower()) < 0)
        {
            string[] tmp = { str };
            return tmp;
        }
        return Regex.Split(str, Regex.Escape(s), RegexOptions.IgnoreCase);
    }
    #endregion
    #endregion

    #region 转换相关
    public static DateTime Ext_ToDateTime(this object Expression)
    {
        return Expression.Ext_ToDateTime(DateTime.Now);
    }
    /// <summary>
    /// 转换为DateTime型
    /// </summary>
    /// <param name="Expression"></param>
    /// <param name="defValue"></param>
    /// <returns></returns>
    public static DateTime Ext_ToDateTime(this object Expression, DateTime defValue)
    {
        if (null != Expression)
        {
            DateTime time;
            if (DateTime.TryParse(Expression.ToString().Trim(), out time))
                return time;
        }
        return defValue;
    }
    /// <summary>
    /// string型转换为bool型
    /// </summary>
    /// <param name="strValue">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的bool类型结果</returns>
    public static bool Ext_ToBool(this object Expression, bool defValue)
    {
        return TypeParse.StrToBool(Expression, defValue);
    }

    /// <summary>
    /// 将对象转换为Int64类型
    /// </summary>
    /// <param name="Expression">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的long类型结果</returns>
    public static long Ext_ToLong(this object Expression, long defValue)
    {
        return TypeParse.StrToLong(Expression, defValue);
    }

    /// <summary>
    /// 将对象转换为Int64类型
    /// </summary>
    /// <param name="Expression"></param>
    /// <returns></returns>
    public static long Ext_ToLong(this object Expression)
    {
        return TypeParse.StrToLong(Expression, 0);
    }

    /// <summary>
    /// 将对象转换为Int32类型
    /// </summary>
    /// <param name="strValue">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的int类型结果</returns>
    public static int Ext_ToInt(this object Expression, int defValue)
    {
        return TypeParse.StrToInt(Expression, defValue);
    }
    /// <summary>
    /// 将对象转换为Int32类型
    /// </summary>
    /// <param name="Expression"></param>
    /// <returns></returns>
    public static int Ext_ToInt(this object Expression)
    {
        return Expression.Ext_ToInt(0);
    }
    /// <summary>
    /// string型转换为float型
    /// </summary>
    /// <param name="strValue">要转换的字符串</param>
    /// <param name="defValue">缺省值</param>
    /// <returns>转换后的int类型结果</returns>
    public static float Ext_ToFloat(this object strValue, float defValue)
    {
        return TypeParse.StrToFloat(strValue, defValue);
    }
    /// <summary>
    /// 将全角数字转换为数字
    /// </summary>
    /// <param name="SBCCase"></param>
    /// <returns></returns>
    public static string Ext_SBCCaseToNumberic(this string SBCCase)
    {
        char[] c = SBCCase.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
            if (b.Length == 2)
            {
                if (b[1] == 255)
                {
                    b[0] = (byte)(b[0] + 32);
                    b[1] = 0;
                    c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                }
            }
        }
        return new string(c);
    }
    #region  转换数组为字符串格式
    /// <summary>
    /// 转换数组为字符串格式
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public static string Ext_ArrayToString(this Int32[] x)
    {
        return x.Ext_ArrayToString(",");
    }
    /// <summary>
    /// 转换数组为字符串格式
    /// </summary>
    /// <param name="o"></param>
    /// <param name="_Interval"></param>
    /// <returns></returns>
    public static string Ext_ArrayToString(this object o, string _Interval)
    {
        if (null == o) return "";
        StringBuilder _ReturnStr = new StringBuilder();
        if (o.GetType() == typeof(int[]))
        {
            Int32[] _newArray = (Int32[])o;
            for (int i = 0; i < _newArray.Length; i++)
            {
                _ReturnStr.Append(_newArray[i].ToString() + _Interval);
            }
        }
        else if (o.GetType() == typeof(string[]))
        {
            string[] _newArray = (string[])o;
            for (int i = 0; i < _newArray.Length; i++)
            {
                _ReturnStr.Append(_newArray[i].ToString() + _Interval);
            }
        }
        if (0 != _ReturnStr.Length)
            _ReturnStr.Remove(_ReturnStr.ToString().LastIndexOf(_Interval), _Interval.Length);
        return _ReturnStr.ToString();
    }
    #endregion

    #region 实现Lucene转义
    /// <summary>
    /// 实现Lucene转义
    /// </summary>
    /// <param name="_str"></param>
    /// <returns></returns>
    static public string Ext_LuceneEscape(this string _str)
    {
        //+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
        return System.Text.RegularExpressions.Regex.Replace(_str, @"(\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\~|\*|\?|\:|\\|\"")+", @"\$1");
    }
    #endregion
    #region 正则 Escape
    /// <summary>
    /// 正则转义
    /// </summary>
    /// <param name="_str"></param>
    /// <returns></returns>
    static public string Ext_Escape(this string _str)
    {
        return System.Text.RegularExpressions.Regex.Escape(_str);
    }
    #endregion
    #region 全文索引转义
    /// <summary>
    /// 全文索引转义
    /// </summary>
    /// <param name="_str"></param>
    /// <returns></returns>
    static public string Ext_MsSQL_Escape(this string _str)
    {
        _str = System.Text.RegularExpressions.Regex.Replace(_str, @"(\"")+", @"""$1");
        _str = string.Format("\"{0}\"", _str);
        return _str;
    }
    #endregion
    #endregion

    #region 加密解密
    #region MD5加密
    /// <summary>
    /// MD5函数
    /// </summary>
    /// <param name="str">原始字符串</param>
    /// <param name="b">true返回大写，false返回小写</param>
    /// <returns></returns>
    public static string Ext_Md5(this string str, bool isbool)
    {
        if (null == str) return null;
        byte[] b = Encoding.Default.GetBytes(str);
        b = new MD5CryptoServiceProvider().ComputeHash(b);
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < b.Length; i++)
            ret.Append(b[i].ToString("x").PadLeft(2, '0'));
        return ((isbool) ? ret.ToString().ToUpper() : ret.ToString().ToLower());
    }
    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="str">原始字符串</param>
    /// <returns>返回md5加密字符串（小写）</returns>
    public static string Ext_Md5(this string str)
    {
        return str.Ext_Md5(false);
    }
    #endregion

    /// <summary>
    /// js加密
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string Ext_escape(this string s)
    {
        return Microsoft.JScript.GlobalObject.escape(s);
    }

    /// <summary>
    /// js解密
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string Ext_unescape(this string s)
    {
        return Microsoft.JScript.GlobalObject.unescape(s);
    }
    #endregion

    #region 汉字转拼音类
    /// <summary>
    /// 定义拼音区编码数组
    /// </summary>
    private static int[] pyValue = new int[]
                {
                -20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,
                -20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,
                -19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,
                -19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,
                -19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,
                -19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,
                -18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,
                -18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012,
                -17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,
                -17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,
                -17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,
                -16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,
                -16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,
                -16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,
                -15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,
                -15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,
                -15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,
                -15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109,
                -14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902,
                -14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654,
                -14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345,
                -14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112,
                -14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907,
                -13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,
                -13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343,
                -13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076,
                -13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831,
                -12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300,
                -12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798,
                -11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067,
                -11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832,
                -10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328,
                -10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254
                };
    /// <summary>
    /// 定义数组
    /// </summary>
    private static string[] pyName = new string[]
                {
                "A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",
                "Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can",
                "Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng",
                "Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong",
                "Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De",
                "Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui",
                "Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo",
                "Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong",
                "Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han",
                "Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan",
                "Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing",
                "Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke",
                "Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo",
                "La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang",
                "Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun",
                "Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian",
                "Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang",
                "Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning",
                "Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan",
                "Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po",
                "Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu",
                "Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou",
                "Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen",
                "Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu",
                "Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan",
                "Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian",
                "Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai",
                "Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao",
                "Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang",
                "Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun",
                "Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan",
                "Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan",
                "Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"
                };
    /// <summary>
    /// 把汉字转换成拼音(全拼)
    /// </summary>
    /// <param name="hzString">待转换字符串</param>
    /// <returns>转换后的结果</returns>
    public static string Ext_ChineseToPinyin(this string hzString)
    {
        return ChineseToPinyin(hzString, null);
    }
    /// <summary>
    /// 把汉字转换成拼音(全拼)
    /// </summary>
    /// <param name="hzString">待转换字符串</param>
    /// <param name="maxLength">需要转换的长度</param>
    /// <returns>转换后的结果</returns>
    public static string Ext_ChineseToPinyin(this string hzString, int maxLength)
    {
        return ChineseToPinyin(hzString, maxLength);
    }
    /// <summary>
    /// 把汉字转换成拼音(全拼)
    /// </summary>
    /// <param name="hzString">汉字字符串</param>
    /// <returns>转换后的拼音(全拼)字符串</returns>
    private static string ChineseToPinyin(string hzString, int? maxLength)
    {

        if (string.IsNullOrEmpty(hzString))
            return null;

        int _MaxLength = 0;
        if (null != maxLength)
        {
            _MaxLength = (int)maxLength;
            if (_MaxLength <= 1)
                _MaxLength = 10;
            hzString = hzString.Ext_SubString(_MaxLength);
        }

        Regex regex = new Regex(@"([a-zA-Z0-9\._]+)", RegexOptions.IgnoreCase);
        if (regex.IsMatch(hzString))
        {
            if (hzString.Equals(regex.Match(hzString).Groups[1].Value, StringComparison.OrdinalIgnoreCase))
                return hzString;
        }
        // 匹配中文字符
        regex = new Regex("^[\u4e00-\u9fa5]$");
        byte[] array = new byte[2];
        StringBuilder pyString = new StringBuilder();
        int chrAsc = 0;
        int i1 = 0;
        int i2 = 0;
        char[] noWChar = hzString.ToCharArray();

        for (int j = 0; j < noWChar.Length; j++)
        {
            // 中文字符
            if (regex.IsMatch(noWChar[j].ToString()))
            {
                array = System.Text.Encoding.Default.GetBytes(noWChar[j].ToString());
                i1 = (short)(array[0]);
                i2 = (short)(array[1]);
                chrAsc = i1 * 256 + i2 - 65536;
                if (chrAsc > 0 && chrAsc < 160)
                {
                    pyString.AppendFormat("_{0}", noWChar[j]);
                }
                else
                {
                    // 修正部分文字
                    if (chrAsc == -9254) // 修正“圳”字
                        pyString.AppendFormat("_Zhen");
                    else
                    {
                        for (int i = (pyValue.Length - 1); i >= 0; i--)
                        {
                            if (pyValue[i] <= chrAsc)
                            {
                                pyString.AppendFormat("{0}", pyName[i]);
                                break;
                            }
                        }
                    }
                }
            }
            // 非中文字符
            else
            {
                pyString.Append(noWChar[j]);
            }
        }
        pyString = pyString.Replace(" ", "_").Replace("?", "_")
            .Replace("\\", "_").Replace("/", "_")
            .Replace(":", "_").Replace("*", "_")
            .Replace(">", "_").Replace("<", "_")
            .Replace("？", "_").Replace("|", "_")
            .Replace("\"", "_").Replace("(", "_")
            .Replace(")", "_").Replace(";", "_");
        pyString = pyString.Replace("，", ",").Replace("；", "_")
            .Replace("。", "_").Replace("”", "_")
            .Replace("“", "_").Replace("[", "_")
            .Replace("]", "_").Replace("【", "_").Replace("】", "_");
        pyString = pyString.Replace("{", "_").Replace("}", "_")
            .Replace("^", "_").Replace("&", "_")
            .Replace("=", "_").Replace("~", "_").Replace("@", "_").Replace("￥", "_");
        pyString = pyString.Replace("！", "_");
        string _pyStr = pyString.ToString().Trim();
        while (true)
        {
            bool _IsOver1 = false, _IsOver2 = false, _IsOver3 = false;
            if (_pyStr.StartsWith("_"))
                _pyStr = _pyStr.Substring(1);
            else
                _IsOver1 = true;
            if (_pyStr.EndsWith("_"))
                _pyStr = _pyStr.Substring(0, _pyStr.Length - 1);
            else
                _IsOver2 = true;
            if (-1 < _pyStr.IndexOf("__"))
                _pyStr = _pyStr.Replace("__", "_");
            else
                _IsOver3 = true;

            if (_IsOver1 && _IsOver2 && _IsOver3)
                break;
        }
        return _pyStr;
    }
    #endregion
}
